package mm.chenchen.modules.reptile.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import mm.chenchen.common.utils.FileUtils;
import mm.chenchen.common.utils.MinioClientUtil;
import mm.chenchen.modules.file.entity.FileInfoEntity;
import mm.chenchen.verification.Query;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import mm.chenchen.modules.reptile.entity.ReptileNetbianImage;
import mm.chenchen.modules.reptile.service.ReptileNetbianImageService;
import mm.chenchen.modules.reptile.dao.model.request.ReptileNetbianImageQuery;
import mm.chenchen.utils.PageUtils;
import mm.chenchen.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 *
 * @author 
 * @email 
 * @date 2021-07-13 17:48:31
 */
@RestController
@Api(tags = "爬虫图片管理")
@RequestMapping("reptile/reptilenetbianimage")
@CrossOrigin
public class ReptileNetbianImageController {
    @Autowired
    private ReptileNetbianImageService service;
    @Autowired
    private MinioClientUtil minioClientUtil;

    @ApiOperation(value = "上传文件", notes = "支持多个文件上传")
    @PostMapping("/upload")
    public mm.chenchen.common.utils.R uploadFile(@RequestParam String fileName,@RequestParam String imageType, HttpServletRequest request) {
        ServletFileUpload.isMultipartContent(request);
        MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
        Map<String, MultipartFile> filemm = multipartRequest.getFileMap();
        Iterator<MultipartFile> ii = filemm.values().iterator();
        while (ii.hasNext()) {
            FileInfoEntity fileInfoEntity = FileUtils.uploadFileToMinio(minioClientUtil.getMinioClient(), ii.next(),"netbian");
            if (fileInfoEntity != null) {
                ReptileNetbianImage image=new ReptileNetbianImage();
                image.setMinioId(fileInfoEntity.getMinioId());
                image.setFileName(fileName);
                image.setFileType(fileInfoEntity.getFileType());
                image.setCreateTime(new Date());
                image.setBuckets(fileInfoEntity.getFileType());
                image.setImageType(imageType);
                service.saveInfo(image);
            }
        }
        return mm.chenchen.common.utils.R.ok();
    }

    @ApiOperation(value = "类型下拉框", notes = "")
    @GetMapping("/drop/list")
    public R getAllType(){
        return service.getAllType();
    }

    /**
     * 列表
     */
    @ApiOperation(value = "分页检索", notes = "")
    @PostMapping("/list")
    //@RequiresPermissions("reptile:reptilenetbianimage:list")
    public R findPage(@RequestBody @Validated({Query.class}) ReptileNetbianImageQuery param){
        return service.findPage(PageUtils.getPage(param), param);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "单个检索 ", notes = "")
    @GetMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:reptilenetbianimage:info")
    public R getById(@ApiParam(name = "objId", value = "") @PathVariable Integer objId){
        return service.getById(objId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增", notes = "")
    @PostMapping(value = "", produces = "application/json")
    //@RequiresPermissions("reptile:reptilenetbianimage:save")
    public R save(@RequestBody ReptileNetbianImage param){
        return service.saveInfo(param);
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新", notes = "")
    @PutMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:reptilenetbianimage:update")
    public R update(@ApiParam(name = "objId", value = "") @PathVariable Integer objId,@RequestBody ReptileNetbianImage param){
        param.setId(objId);
        return service.update(param);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping(value = "/{objId}", produces = "application/json")
    //@RequiresPermissions("reptile:reptilenetbianimage:delete")
    public R deleteById(@ApiParam(name = "objId", value = "") @PathVariable Integer objId){
        return service.deleteById(objId);
    }

}
